package com.etc.order.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.etc.order.common.domain.ProductCategory;
import com.etc.order.common.dto.CategoryDTO;
import com.etc.order.common.dto.SimpleDTO;
import com.etc.order.common.service.ProductCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@Controller
@CrossOrigin
public class CategoryController {
    @Resource
    @Reference
    private ProductCategoryService productCategoryService;

    @GetMapping("/category.html")
    public String category() {
        return "category";
    }

    @GetMapping("/category/list")
    @ResponseBody
    public SimpleDTO getCategoryList(String categoryIdOrName, int pageIndex, int pageSize) {
        List<ProductCategory> productCategories;
        int totalCount = 0;
        if (categoryIdOrName != null && !categoryIdOrName.equals("")) {
            try {
                int categoryId = Integer.parseInt(categoryIdOrName);
                ProductCategory productCategory = productCategoryService.getOne(categoryId);
                productCategories = new ArrayList<>();
                if (productCategory != null) {
                    productCategories.add(productCategory);
                    totalCount = 1;
                }
            } catch (NumberFormatException e) {
                productCategories = productCategoryService.findByCategoryName(categoryIdOrName, pageIndex, pageSize);
                totalCount = productCategoryService.getTotalCount(categoryIdOrName);
            }
        } else {
            productCategories = productCategoryService.findAllProductCategory(pageIndex, pageSize);
            totalCount = productCategoryService.getTotalCount();
        }
        List<Object> productCategoryDTOList = new ArrayList<>();
        for (ProductCategory category : productCategories) {
            CategoryDTO categoryDTO = new CategoryDTO();
            categoryDTO.setCategoryId(category.getCategoryId());
            categoryDTO.setName(category.getCategoryName());
            categoryDTO.setCategoryType(category.getCategoryType());
            productCategoryDTOList.add(categoryDTO);
        }
        int totalPage = totalCount / pageSize;
        productCategoryDTOList.add(totalCount % pageSize == 0 ? totalPage : totalPage + 1);
        return new SimpleDTO(0, "成功", productCategoryDTOList);
    }

    @GetMapping("/sell/category/save")
    @ResponseBody
    public SimpleDTO saveCategory(String categoryId, String categoryName, int categoryType) {
        try {
            categoryName = URLDecoder.decode(categoryName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        ProductCategory category = new ProductCategory();
        if (!categoryId.equals("")) category.setCategoryId(Integer.parseInt(categoryId));
        category.setCategoryName(categoryName);
        category.setCategoryType(categoryType);
        int result = productCategoryService.save(category);
        if (result > 0) {
            return new SimpleDTO(0, "成功", null);
        } else {
            return new SimpleDTO(1, "失败", null);
        }
    }

    @GetMapping("/sell/category/delete")
    @ResponseBody
    public SimpleDTO deleteCategory(int categoryId) {
        if (productCategoryService.delete(categoryId) > 0) {
            return new SimpleDTO(0, "成功", null);
        } else {
            return new SimpleDTO(1, "失败", null);
        }
    }
}
